package top.korori.sv.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import org.springframework.beans.factory.annotation.Autowired;
import top.korori.common.utils.RedisCache;
import top.korori.sv.constant.CacheConstant;
import top.korori.sv.entity.SvUserArea;
import top.korori.sv.entity.vo.SvUserAreaTreeVo;
import top.korori.sv.mapper.SvUserAreaMapper;
import top.korori.sv.service.SvUserAreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 * 前台用户所在地地区库 服务实现类
 * 
 *
 * @author korori
 *
 */
@Service
public class SvUserAreaServiceImpl extends ServiceImpl<SvUserAreaMapper, SvUserArea> implements SvUserAreaService {

    @Autowired
    private RedisCache redisCache;

    @Override
    public List<SvUserAreaTreeVo> tree() {

        List<SvUserAreaTreeVo> cacheData  =  redisCache.get(CacheConstant.SV_USER_AREA_TREE);
        if (ObjUtil.isNotEmpty(cacheData)){
            return cacheData;
        }

        List<SvUserArea> list = this.list();

        List<SvUserAreaTreeVo> userAreaTree = BeanUtil.copyToList(list, SvUserAreaTreeVo.class);

        List<SvUserAreaTreeVo> parentList = userAreaTree.stream()
                .filter(item -> ObjUtil.equals(item.getParentId(), 0))
                .collect(Collectors.toList());

        for (SvUserAreaTreeVo svUserAreaTreeVo : parentList) {
            List<SvUserAreaTreeVo> collect = userAreaTree.stream()
                    .filter(item -> ObjUtil.equals(item.getParentId(), svUserAreaTreeVo.getAreaId()))
                    .collect(Collectors.toList());
            svUserAreaTreeVo.setChildren(collect);
        }

        redisCache.set(CacheConstant.SV_USER_AREA_TREE,parentList);

        return parentList;
    }

    @Override
    public List<Integer> isExistArea(String province, String city) {
        List<SvUserAreaTreeVo> tree = tree();
        SvUserAreaTreeVo svUserAreaTreeVoProvince = tree.stream()
                .filter(item -> ObjUtil.equals(province, item.getName()))
                .findFirst()
                .orElse(null);

        if (ObjUtil.isNull(svUserAreaTreeVoProvince)) {
            return new ArrayList<>();
        }


        SvUserAreaTreeVo svUserAreaTreeVoCity = svUserAreaTreeVoProvince.getChildren().stream()
                .filter(item -> ObjUtil.equals(city, item.getName()))
                .findFirst()
                .orElse(null);

        if (ObjUtil.isNull(svUserAreaTreeVoCity)) {
            return new ArrayList<>();
        }

        ArrayList<Integer> ids = new ArrayList<>();
        ids.add(svUserAreaTreeVoProvince.getAreaId());
        ids.add(svUserAreaTreeVoCity.getAreaId());
        return ids;
    }

    @Override
    public Integer isProvince(String province) {
        List<SvUserAreaTreeVo> tree = tree();
        return tree.stream()
                .filter(item -> ObjUtil.equals(province, item.getName()))
                .map(SvUserAreaTreeVo::getAreaId)
                .findFirst()
                .orElse(null);
    }
}

























